<?php
namespace Admin\Model;
use \Frame\Libs\BaseModel;

final class ActivityModel extends BaseModel{
	//获取记录数
	public function rowCount(){
		$sql = "SELECT activity_id FROM love_activity";
		return $this->pdo->rowCount($sql);
	}
	
	//分页获取记录
	public function fetchByPages($startrows,$pagesize){
		$sql = "SELECT * FROM love_activity LIMIT $startrows,$pagesize";
		return $this->pdo->fetchAll($sql);
	}
	
	//获取具体某条记录
	public function fetchOne($id){
		$sql = "SELECT * FROM love_activity WHERE activity_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
	
	//插入新的活动（基本信息）
	public function addActivity($name,$place,$a,$b,$c,$re,$begin,$money,$type){
		$sql = "INSERT INTO love_activity (activity_name,activity_place,activity_partner_a,activity_partner_b,activity_partner_c,activity_recipient,activity_begin,activity_money,activity_type) VALUES ('$name','$place','$a','$b','$c','$re','$begin','$money','$type')";
		$a = $this->pdo->exec($sql);
		if($a==1){
			return $this->pdo->lastInsertId();
		}
		else{
			return 0;
		}
	}
	
	//向活动图片表插入
	public function addImg($pic,$activity_id){
		$sql = "INSERT INTO love_activity_image (activity_id,image) VALUES ('$activity_id','$pic')";
		return $this->pdo->exec($sql);
	}
	
	//获取状态
	public function getStatus($id){
		$sql = "SELECT activity_status FROM love_activity WHERE activity_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
	
	//插入捐书信息
	public function addActivityBook($activity_id,$book_id,$book_name,$book_count){
		$sql = "INSERT INTO love_book_whereabouts (activity_id,book_id,book_name,book_count) VALUES ('$activity_id','$book_id','$book_name','$book_count')";
		return $this->pdo->exec($sql);
	}
	
	//计算某活动的捐书总数
	public function countBook($activity_id){
		$sql = "SELECT SUM(book_count) FROM love_book_whereabouts WHERE activity_id='$activity_id'";
		return $this->pdo->fetchAll($sql);
	}
	
	//把捐书总数更新到activity表的字段中
	public function updateBookCount($activity_id,$sum){
		$sql = "UPDATE love_activity SET activity_book='$sum' WHERE activity_id='$activity_id'";
		return $this->pdo->exec($sql);
	}
	
	//获取某活动的捐书明细
	public function fetchActivityBook($activity_id){
		$sql = "SELECT * FROM love_book_whereabouts WHERE activity_id='$activity_id'";
		return $this->pdo->fetchAll($sql);
	}
	
	//修改活动
	public function editActivity($id,$name,$place,$a,$b,$c,$re,$begin,$end,$book,$money,$status,$type){
		$sql = "UPDATE love_activity SET activity_name='$name',activity_place='$place',activity_partner_a='$a',activity_partner_b='$b',activity_partner_c='$c',activity_recipient='$re',activity_begin='$begin',activity_end='$end',activity_book='$book',activity_money='$money',activity_status='$status',activity_type='$type' WHERE activity_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//删除某活动的某本书
	public function delActivityBook($id){
		$sql = "DELETE FROM love_book_whereabouts WHERE book_whereabouts_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//插入money_whereabouts
	public function addmoney($id,$money){
		$sql = "INSERT INTO love_money_whereabouts (activity_id,money_count) VALUES ('$id','$money')";
		return $this->pdo->exec($sql);
	}
	
	//编辑money_whereabouts
	public function editmoney($id,$money){
		$sql = "UPDATE love_money_whereabouts SET money_count='$money' WHERE activity_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//得出书籍总量
	public function getBookSum($id){
		$sql = "SELECT book_out+book_inventory AS sum FROM love_book WHERE book_id='$id'";
		return $this->pdo->fetchOne($sql);
	}
	
	//得出去向表中总数
	public function getBookOut($id){
		$sql = "SELECT SUM(book_count) FROM love_book_whereabouts WHERE book_id='$id'";
		return $this->pdo->fetchAll($sql);
	}
	
	//删除
	public function delActivity($id){
		$sql = "DELETE FROM love_activity WHERE activity_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//设置状态的三个方法
	public function setOn($id){
		$sql = "UPDATE love_activity SET activity_status='1' WHERE activity_id='$id'";
		return $this->pdo->exec($sql);
	}
	public function setOff($id){
		$sql = "UPDATE love_activity SET activity_status='2',activity_end=now() WHERE activity_id='$id'";
		return $this->pdo->exec($sql);
	}
	public function setOther($id){
		$sql = "UPDATE love_activity SET activity_status='3' WHERE activity_id='$id'";
		return $this->pdo->exec($sql);
	}
	
	//在书籍库存中减去
	public function outBook1($book_id,$count){
	   $sql = "UPDATE love_book SET book_inventory=book_inventory-$count WHERE book_id='$book_id'";
	   return $this->pdo->exec($sql);
	}
	//在书籍已捐中加上
	public function outBook2($book_id,$count){
	    $sql = "UPDATE love_book SET book_out=book_out+$count WHERE book_id='$book_id'";
	    return $this->pdo->exec($sql);
	}
	//获取某个活动的金额
	public function getMoney($id){
	    $sql = "SELECT activity_money FROM love_activity WHERE activity_id='$id'";
	    return $this->pdo->fetchOne($sql);
	}
	//在common中减去
	public function outMoney($money){
	    $sql = "UPDATE love_common SET money=money-$money WHERE common_id='1'";
	    return $this->pdo->exec($sql);
	}
}
?>